package com.ctbu.web;

import com.ctbu.entity.LoanProduct;
import com.ctbu.entity.LoanProductType;
import com.ctbu.service.LoanProductTypeService;
import com.ctbu.tools.HttpStatusEnum;
import com.ctbu.tools.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 贷款类型表 前端控制器
 * </p>
 *
 * @author lli
 * @since 2024年06月19日
 */
@RestController
@RequestMapping("/loanProductType")
public class LoanProductTypeController {
    @Autowired
    private LoanProductTypeService loanProductTypeService;
    /**
     * post:增加  put:修改 delete:删除 get:查询
     */

    /**
     * 分页查询 模糊查询
     * @param pageNum 当前页默认1
     * @param pageSize  每页大小默认5
     * @return
     */
    @GetMapping
    public ResponseResult<Object> list(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "5") int pageSize,
            @RequestParam(value = "loanTypeName",required = false,defaultValue = "") String loanTypeName

    ){
        return loanProductTypeService.selectList(pageNum,pageSize,loanTypeName);

    }

    /**
     * 按id查询
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public ResponseResult<LoanProductType> queryLoanProductById(@PathVariable String id){
        LoanProductType loanProductType=loanProductTypeService.getById(id);
        return ResponseResult.success(loanProductType);
    }

    /**
     * @param loanProductType
     * @return
     * @RequestBody 接收前端传递的JSON数据的类型
     */
    @PostMapping
    public ResponseResult<Boolean> save(@RequestBody LoanProductType loanProductType){
        boolean b=loanProductTypeService.save(loanProductType);
        if(b){
            return ResponseResult.success(b,"增加产品类型成功");
        }
        return ResponseResult.error(HttpStatusEnum.INTERNAL_SERVER_ERROR,"增加产品类型失败");
    }

    /**
     * 修改
     * @param loanProductType
     * @return
     */

    @PutMapping
    public ResponseResult<Boolean> update(@RequestBody LoanProductType loanProductType){
        boolean b=loanProductTypeService.updateById(loanProductType);
        if(b){
            return ResponseResult.success(b,"修改产品类型成功");
        }
        return ResponseResult.error(HttpStatusEnum.INTERNAL_SERVER_ERROR,"修改产品类型失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable String id){
        boolean b=loanProductTypeService.removeById(id);
        if(b){
            return ResponseResult.success(b,"删除产品类型成功");
        }
        return ResponseResult.error(HttpStatusEnum.INTERNAL_SERVER_ERROR,"删除产品类型失败");
    }

    /**
     * 查询所有数据（增加或修改显示信息使用）
     * @return
     */
    @GetMapping("/queryAll")
    public ResponseResult<Object> queryAll(){
        List<LoanProductType> list = loanProductTypeService.list();
        return ResponseResult.success(list,"查询成功");

    }

}
